package com.hr;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.espirit.eap.manager.Department;
import com.espirit.eap.manager.DepartmentService;
import com.espirit.eap.sa.Employee;
import com.espirit.eap.util.StatGenericJPAService;
import com.googlecode.cswish.annotation.ParamScope;
import com.googlecode.cswish.exception.ApplicationException;
import com.googlecode.cswish.model.PageElements;
import com.googlecode.cswish.model.PageInfo;
import com.googlecode.cswish.model.ParameterMap;
import com.googlecode.cswish.model.Reference;
import com.googlecode.cswish.oosearch.QLInfo;
import com.googlecode.cswish.struts.spring.SearchCallback;
import com.googlecode.cswish.util.bean.ModelUtil;

@Service
public class SchedualDefineService {
	
	@Resource
	StatGenericJPAService genericService;
	
	@Resource
	DepartmentService departmentService;
	
	
	public  PageInfo search(final ParameterMap model, int start, int limit,
    		String sort, String dir, boolean newSearch, Reference<QLInfo> qlInfoRef,
    		@ParamScope(visible=false, value="#ps")PageElements ps ,boolean fromMenu
    		) {
		
		SearchCallback callback = departmentService.buildDeptDefaultSearchCallback(model, null, null);
		return genericService.search(model, start, limit, sort, dir, newSearch, qlInfoRef, callback, true, false);
	}
	
	public void _add(SchedualDefine model){
		StringBuffer sb = new StringBuffer();
		for(int i =0 ; i < 5 ; i++){
			sb.append(Schedual.WEEK_PREFIX + (i+1)).append(",");
		}
		model.setWeekNbrs(sb.toString());
	}
	
	
	public void add(SchedualDefine model){
		checkInput(model);
		genericService.add(model);
	}
	
	private void checkInput(SchedualDefine model){
		if (model == null) return ;
		String deptIds = model.getDeptIds();
		if (deptIds != null) {
			int pos = deptIds.indexOf(",");
			if (pos > 0){
				throw new ApplicationException("只准选择一个部门");
			} else {
				model.setDepartment(new Department((deptIds)));
			}
		}
		
		String defaultScope = model.getScope();
		if (!StringUtils.isEmpty(defaultScope)){
			checkScope(defaultScope);
		}
		
		List<SchedualScope> schedualScopes = model.getSchedualScopes();
		if (schedualScopes != null && !schedualScopes.isEmpty()){
			for(SchedualScope schedualScope : schedualScopes){
				String scope = schedualScope.getScope();
				checkScope(scope);
			}
		}
		
		List<SchedualEmployeeScope> employeeScopes = model.getEmployeeScopes();
		if (employeeScopes != null && !employeeScopes.isEmpty()){
			for(SchedualEmployeeScope employeeScope : employeeScopes){
				checkScope(employeeScope.getScope());
			}
		}
	}
	
	public void checkScope(String scope){
		if (!StringUtils.isEmpty(scope)){
			String regex = "^\\d{4}-\\d{4}(,\\d{4}-\\d{4})*$";
			boolean valid = scope.matches(regex);
			if (!valid){
				throw new ApplicationException("排班格式不准确：" + scope );
			}
		}
	}
	
	public void _update(SchedualDefine model){
		model = genericService.view(model);
		if (model.getDepartment() != null){
			String deptIds = String.valueOf(model.getDepartment().getId());
			model.setDeptIds(deptIds);
		}
	}
	
	public void update(SchedualDefine model, @ParamScope(visible=false, value="#ps")PageElements ps){
		checkInput(model);
		genericService.update(model);
	}
	
	//TODO: cach it
	public SchedualDefine getSchedualDefineByDeptId(String deptId){
		if (deptId == null) return null;
		
		SchedualDefine example = new SchedualDefine();
		ModelUtil.setProperty(example, "department.id", deptId);
		example = genericService.searchAll(example).getData(0);
		if (example != null){
			return example;
		} else {
			Department parent = departmentService.getParentDepartment(deptId);
			if (parent != null){
				return getSchedualDefineByDeptId(parent.getId());
			}
		}
		return null;
	}
	
	public String getSchedualEmployeeScope(String empId){
		Employee employee = genericService.load(Employee.class, empId);
		if (employee == null) return null;
		String deptId = employee.getId();
		
		SchedualDefine schedualDefine = getSchedualDefineByDeptId(deptId);
		
		return getSchedualEmployeeScope(empId, schedualDefine);
	}
	
	public String getSchedualEmployeeScope(String empId, SchedualDefine schedualDefine){
		Employee employee = genericService.load(Employee.class, empId);
		if (employee == null) return null;
		
		if (schedualDefine != null){
			SchedualEmployeeScope example = null;
			List<SchedualEmployeeScope> employeeScopes = schedualDefine.getEmployeeScopes();
			if (employeeScopes != null){
				for(SchedualEmployeeScope schedualEmployeeScope : employeeScopes){
					if (schedualEmployeeScope.getEmployee().getId().equals(empId)){
						example = schedualEmployeeScope;
						return example.getScope();
					}
				}
				
				return schedualDefine.getScope();
			}
			
		}
		return null;
	}
}
